﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace AscendEast_DataEntry
{
    public partial class Form_School_Edit : Form
    {
        DataAccess dal = new DataAccess();
        public Form_School_Edit()
        {
            InitializeComponent();
            PopulauteComboBox_Schools();
            tb_school_id_school_edit.Enabled = false;
        }

        private void PopulauteComboBox_Schools()
        {
            string select_schools_query = "select * from tbl_schools";
            DataTable dt0 = dal.Select(select_schools_query);
            DataTable dt = new DataTable();
            dt.Columns.Add("school_id", typeof(String));
            dt.Columns.Add("school_name", typeof(String));
            dt.Rows.Add("null", "Select School");
            for (int i = 0; i < dt0.Rows.Count; i++)
            {
                dt.Rows.Add(dt0.Rows[i][0], dt0.Rows[i][1]);
            }

            cb_school_name_edit_search.DisplayMember = dt.Columns["school_name"].ToString();
            cb_school_name_edit_search.ValueMember = dt.Columns["school_id"].ToString();
            cb_school_name_edit_search.DataSource = dt;
        }

        private void btn_search_edit_school_Click(object sender, EventArgs e)
        {
            if (cb_school_name_edit_search.SelectedIndex == 0)
                MessageBox.Show("Please select any School");
            else
            {
                int school_id = int.Parse(cb_school_name_edit_search.SelectedValue.ToString());
                PopulateDataGridView_Schools(school_id);
            }
        }

        private void PopulateDataGridView_Schools(int school_id)
        {
            string edit_schoool_query = "select * from tbl_schools where school_id = " + school_id;
            dgv_school_edit.DataSource = null;
            dgv_school_edit.Rows.Clear();
            dgv_school_edit.Refresh();
            DataTable dt = new DataTable();
            dt = dal.Select(edit_schoool_query);

            for (int i = 0; i < dt.Rows.Count; i++)
            {
                dgv_school_edit.Rows.Add(dt.Rows[i][1]);
            }

            tb_school_id_school_edit.Text = dt.Rows[0][0].ToString();
            tb_school_name_school_edit.Text = dt.Rows[0][1].ToString();
        }

        private void btn_update_school_edit_Click(object sender, EventArgs e)
        {
            if (cb_school_name_edit_search.SelectedIndex == 0)
                MessageBox.Show("Please select any School");
            else
            {

                string school_name_updated = tb_school_name_school_edit.Text;
                string check_duplicate_query = "select * from tbl_schools where school_name = '" + school_name_updated + "'";
                bool duplicate_flag = dal.Check_Duplicate(check_duplicate_query);
                if (duplicate_flag == true)
                    MessageBox.Show("School already exists. Please enter another School");
                else
                {
                    int school_id = int.Parse(tb_school_id_school_edit.Text);
                    string update_state_query = "update tbl_schools set school_name = '" + school_name_updated + "' where school_id = " + school_id;
                    bool updated_flag = dal.Update(update_state_query);
                    if (updated_flag == true)
                        MessageBox.Show("Record Updated Successfully");
                    else
                        MessageBox.Show("Try again");

                    PopulateDataGridView_Schools(school_id);
                    PopulauteComboBox_Schools();
                    school_id = 0;                                      
                    school_name_updated = null;
                    tb_school_id_school_edit.Text = null;
                    tb_school_name_school_edit.Text = null;
                }
            }
        }               
    }
}
